﻿using Google.API.Search;
using System;
using System.Collections.Generic;

namespace BotDetectorApp
{
    public class BotDetector
    {
        public bool AnalyzeAfterExactPhrase(string text, IList<IWebResult> results)
        {
            int exactPhrasesFound = 0;
            foreach (var webResult in results)
            {
                string title = webResult.Title.Replace("\n", string.Empty);
                string content = webResult.Content.Replace("\n", string.Empty);
                if (title.Contains(text) || content.Contains(text))
                {
                    exactPhrasesFound++;
                }
            }
            if (exactPhrasesFound > 5)
            {
                return true;
            }
            return false;
        }
        public bool AnalyzeAfterSentences(string text, IList<IWebResult> results)
        {
            //pe viitor tinem cont si de lungimea propozitiilor, cele mai scurte (vand frigider) se gasesc mai des deci nu inseamna ca-s boti neaparat si ar trebui un numar mai mare
            //dar cele mai lungi care sigur sunt boti se gasesc mai rar deci trebuie un numar mai mic

            //2 abordari si aici: cautare exact sau dupa cuvinte cheie (pt fiecare propozitie)
            int nrOfSentencesFound = 0, nrOfBotResults = 0;
            string[] sentences = text.Split('.');
            foreach (var webResult in results)
            {
                string content = webResult.Content.Replace("\n", string.Empty);
                foreach (string sentence in sentences)
                {
                    if (content.Contains(sentence))
                    {
                        nrOfSentencesFound++;
                    }
                }
                if (nrOfSentencesFound > 0)
                {
                    nrOfBotResults++;
                }
            }
            if (nrOfBotResults > 10)
            {
                return true;
            }
            return false;
        }
        public bool AnalyzeAfterKeyWords(string text, IList<IWebResult> results)
        {
            int nrOfKeyWordsFound = 0, nrOfBotResults = 0;
            string[] separators = { ",", ".", "!", "?", ";", ":", " " };
            foreach (var webResult in results)
            {
                string content = webResult.Content.Replace("\n", string.Empty);
                string[] words = content.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                string[] keyWords = new string[100];
                int i = 0;
                foreach (string word in words)
                {
                    if (word.Length > 4)
                    {
                        keyWords[++i] = word;
                    }
                }

                foreach (var keyWord in keyWords)
                {
                    if (!string.IsNullOrEmpty(keyWord)
                        && content.Contains(keyWord))
                    {
                        nrOfKeyWordsFound++;
                    }
                }
                if ((nrOfKeyWordsFound > (keyWords.Length - keyWords.Length / 6)) &&
                    AnalyzeDistances(content, keyWords))
                {
                    nrOfBotResults++;
                }
            }
            if (nrOfBotResults > 5) //daca strangem lesa aici atunci tre sa o slabim dincolo / reciproc
            {
                return true;
            }
            return false;
        }
        public bool AnalyzeDistances(string content, string[] keyWords)
        {
            string[] separators = keyWords;
            content = content.Replace("\n", string.Empty);
            string[] leftOverWords = content.Split(separators, StringSplitOptions.RemoveEmptyEntries);
            int smallDistanceCounter = 0;
            foreach (string word in leftOverWords)
            {
                if (word.Length < 10) 
                {
                    smallDistanceCounter++;
                }
            }
            if (smallDistanceCounter / leftOverWords.Length > 0.4)
            {
                return true;
            }
            return false;
        }
    }
}
